<!DOCTYPE html>
<html>

<head>
	<meta charset="UTF-8">
	<title>Insert title here</title>
	<script type="text/javascript" src="../jquery-3.7.1.min.js"></script>
	<script type="text/javascript">
		window.onload = function () {
			$("#login").click(function () {
				$.get("http://a.test.com:8080/user/login", function (data, status) {
					const obj = JSON.parse(data);
					const token = obj.token
					alert("token is: " + token);
					document.cookie = "token=" + token + "; domain=.test.com; path=/";
				});
			});
			$("#token").click(function () {
				var token = getCookie("token");
				alert("token is: " + token);
			});
			// 在第二个页面读取 cookie
			function getCookie(name) {
				var nameEQ = name + "=";
				var ca = document.cookie.split(';');
				for (var i = 0; i < ca.length; i++) {
					var c = ca[i];
					console.log("c is: " + c);
					while (c.charAt(0) == ' ') 
						c = c.substring(1, c.length);
					if (c.indexOf(nameEQ) == 0)
						var result = c.substring(nameEQ.length, c.length);  
						return result;
				}
				return null;
			}
		}
	</script>
</head>

<body>
	<input id="login" type="button" style="margin:100px;" value="登录" />
	<input id="token" type="button" value="获取token" />
</body>

</html>